


********************************************************************************************************************************************
*This .do file takes the monthly PM25 data and finds the school-year PM2.5 average ****caused by electricity generation only.***  It then takes all public schools and finds the nearest 0.1 degree by 0.1 degree
*cell to it and reports that as the school-year pollution from electricity production only faced by the school.  PM2.5 Power Generation Exposure averages and PM2.5 Power Generation Exposure gaps can then be calculated using enrollment weights.
*Note this .do file only does the PM2.5 exposure and gaps caused by electricity generation, not total PM2.5. For those results, run "Table2_PanelB_1.do"

*Inputs: 
*1. "Raw Data\7. Table 2 Data\Hernandez Data\census_tract_year_pm25_from_electricity.dta: PM2.5 from electricity generation from Hernandez et al. (2022).  These data can be downloaded here: https://github.com/hernandezcortes/HCMW_EJ_trends_electricity
*2. "Raw Data\7. Table 2 Data\Hernandez Data\Merged\database.dta" and "Raw Data\7. Table 2 Data\Hernandez Data\Merged\coord.dta". These are generated using the census tract files from the Census bureau. They are available here: https://www.census.gov/cgi-bin/geo/shapefiles/index.php?year=2010&layergroup=Census+Tracts (we downloaded each state's file for 2000)
*3. "Raw Data\7. Table 2 Data\Schools Data\schoolsdata.dta": School enrollments and school locations from the NCES for school years 2004-05, 2011-12, and 2018-19.  These data were created using the NCES Table generator available at https://nces.ed.gov/ccd/elsi/tablegenerator.aspx.

*Outputs the following results:
*1. Panel B of Table 2 (`Power Generation Exposure' and `Power Generation Exposure Gap' rows only)
********************************************************************************************************************************************

clear all
set more off
cd "C:\Users\gilraine\Dropbox\PM25_test_draft\R&R\ReplicationPackage\Derived Data\Table 2 Data"

*This is done manually one year at a time.  
*Manual process: Change one line to your desired year. Specifically, change:
*1. Change year number on line 27 (see note on line 26)

*Change the year to desired year (2005, 2012, or 2019). 
local year=2005
local year_p=`year'-1

*Save temp file with just year
clear all
use  "Hernandez Data\census_tract_year_pm25_from_electricity.dta" 
*Only keep if year of analysis
*Note that the Hernandez et al data does not cover 2019.  We thus use their 2018 data in place of 2019.
keep if year==`year_p'
save "Hernandez Data\temp_`year'.dta", replace

clear all
use "Schools Data\schoolsdata.dta" 
*Match year; only cleaned schools data for 2005, 2010, 2019
keep if year==`year'
geoinpoly lat lon using "Hernandez Data\Merged\coord.dta" 

merge m:1 _ID using "Hernandez Data\Merged\database.dta" 
keep if _merge==3
drop _merge

merge m:1 gisjoin using "Hernandez Data\temp_`year'.dta" 
drop if _merge==2
drop _merge
erase "Hernandez Data\temp_`year'.dta"

qui su totalpm25 [aw=enroll]
di "PM2.5 Exposure from Electricity Only:"
di r(mean)
qui su totalpm25 [aw=black]
local b=r(mean)
qui su totalpm25 [aw=white]
local w=r(mean)
di "PM2.5 B-W Exposure Gap from Electricity Only:"
di `b'-`w'

*****RESULTS********
*Total PM2.5 Exposure from Electricity Only
*2005: 2.1351328
*2012: 0.9081699
*2019: 0.2741429 

*PM2.5 B-W Exposure Gap from Electricity Only
*2005: 0.66434422
*2012: 0.19982424
*2019: 0.03175752


